package cn.tedu.buffer;

import java.nio.ByteBuffer;

public class ByteBufferDemo2 {

    public static void main(String[] args) {

        // 数据已知
        // ByteBuffer buffer = ByteBuffer.wrap("hello big2006".getBytes());
        // System.out.println(buffer.position());
        // 会将缓冲区底层的字节数组给返回来
        // byte[] data = buffer.array();
        // System.out.println(new String(data));

        // 适合于数据未知的场景
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("hello big2006".getBytes());
        byte[] data = buffer.array();
        // System.out.println(new String(data, 0, buffer.position()));
        buffer.flip();
        System.out.println(new String(data, 0, buffer.limit()));
    }

}
